﻿using BCCommon;
using BCCommon.Enums;
using BCData.Trading.Purchase.GoodsPurchasePaidRecord;
using BCDto.Trading.Purchase.GoodsPurchasePaidRecord;
using BCEntity.Trading.Purchase.GoodsPurchasePaidRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCCommon.TradingEnums;
using BCData.Trading.Purchase.GoodsPurchase;
using BCData.Trading.Purchase.GoodsPurchaseChecklist;

namespace BCService.Trading.Purchase.GoodsPurchasePaidRecord
{
    public class GoodsPurchasePaidRecordService : IGoodsPurchasePaidRecordService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IApplicationContextService applicationContextService;
        private readonly IRedisService redisService;
        private readonly IGoodsPurchasePaidRecordData goodsPurchasePaidRecordData;
        private readonly IGoodsPurchaseData goodsPurchaseData;
        private readonly IGoodsPurchaseChecklistData goodsPurchaseChecklistData;
        private readonly string key = string.Format("{0}", EntityNameConst.GoodsPurchasePaidRecordEntity);

        public GoodsPurchasePaidRecordService(IDatabaseContext databaseContext,
            IApplicationContextService applicationContextService,
            IRedisService redisService,
            IGoodsPurchasePaidRecordData goodsPurchasePaidRecordData,
            IGoodsPurchaseData goodsPurchaseData,
            IGoodsPurchaseChecklistData goodsPurchaseChecklistData)
        {
            this.databaseContext = databaseContext;
            this.applicationContextService = applicationContextService;
            this.redisService = redisService;
            this.goodsPurchasePaidRecordData = goodsPurchasePaidRecordData;
            this.goodsPurchaseData = goodsPurchaseData;
            this.goodsPurchaseChecklistData = goodsPurchaseChecklistData;
        }

        #region 检查商品采购支付记录是否存在
        public bool Exists(long goodsPurchasePaidRecordId)
        {
            return this.goodsPurchasePaidRecordData.Exists(goodsPurchasePaidRecordId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加商品采购支付记录
        public GoodsPurchasePaidRecordDto Add(GoodsPurchasePaidRecordRequestDto requestDto)
        {
            if (!this.goodsPurchaseData.Exists(requestDto.GoodsPurchaseId).Result)
            {
                throw new ArgumentException("此采购单信息不存在!");
            }
            if (requestDto.PaidType == PurchasePaidType.LastPayment)
            {
                if (requestDto.GoodsPurchaseChecklistId.HasValue)
                {
                    if (!this.goodsPurchaseChecklistData.Exists(requestDto.GoodsPurchaseChecklistId.Value).Result)
                    {
                        throw new ArgumentException("此商品采购清单信息不存在!");
                    }
                }
            }
            var entity = requestDto.As<GoodsPurchasePaidRecordEntity>();
            entity.CreateAdminName = applicationContextService.Admin.FullName;
            entity.CreateAdminId = applicationContextService.Admin.AdminId;
            var result = this.goodsPurchasePaidRecordData.Add(entity).GetAwaiter().GetResult();
            return result.As<GoodsPurchasePaidRecordDto>();
        }
        #endregion

        #region 修改商品采购支付记录
        public GoodsPurchasePaidRecordDto Update(GoodsPurchasePaidRecordPutDto putDto)
        {
            if (!this.Exists(putDto.GoodsPurchasePaidRecordId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (putDto.PaidType == PurchasePaidType.LastPayment)
            {
                if (putDto.GoodsPurchaseChecklistId.HasValue)
                {
                    if (!this.goodsPurchaseChecklistData.Exists(putDto.GoodsPurchaseChecklistId.Value).Result)
                    {
                        throw new ArgumentException("此商品采购清单信息不存在!");
                    }
                }
            }
            var entity = putDto.As<GoodsPurchasePaidRecordEntity>();
            entity.CreateAdminName = applicationContextService.Admin.FullName;
            entity.CreateAdminId = applicationContextService.Admin.AdminId;
            var result = this.goodsPurchasePaidRecordData.Update(entity).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, entity.GoodsPurchasePaidRecordId);
            this.redisService.DeleteAsync(cacheKey);
            return result.As<GoodsPurchasePaidRecordDto>();
        }
        #endregion

        #region 删除商品采购支付记录
        public bool Delete(long goodsPurchasePaidRecordId)
        {
            if (!this.Exists(goodsPurchasePaidRecordId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.goodsPurchasePaidRecordData.Delete(goodsPurchasePaidRecordId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, goodsPurchasePaidRecordId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取商品采购支付记录
        public GoodsPurchasePaidRecordDto Get(long goodsPurchasePaidRecordId)
        {
            string cacheKey = this.redisService.GetKey(key, goodsPurchasePaidRecordId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.goodsPurchasePaidRecordData.GetEntity(goodsPurchasePaidRecordId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<GoodsPurchasePaidRecordDto>();
        }
        #endregion

        #region 获取商品采购支付记录分页
        public PageableList<GoodsPurchasePaidRecordDto> Query(long goodsPurchaseId, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, goodsPurchaseId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.goodsPurchasePaidRecordData.Query(goodsPurchaseId,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<GoodsPurchasePaidRecordDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<GoodsPurchasePaidRecordDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}